export red="\033[31m"
export green="\033[32m"
export yellow="\033[33m"
export blue="\033[34m"
export purple="\033[35m"
export cyan="\033[36m"
export white="\033[37m"
export background="\033[0m"

cs(){
export HOME="$(pwd)"
if ! grep -q $HOME GitBot.sh;then
    sed "s/HOME_PATH/$HOME/g" *.sh
fi
}
if [ -d $HOME/NodeJS ];then
    export PATH=$PATH:$HOME/NodeJS
    if [ ! -d $HOME/.local/share/pnpm ];then
        mkdir -p $HOME/NodeJS/.local/share/pnpm
    fi
    export PATH=$PATH:$HOME/NodeJS/.local/share/pnpm
    export PNPM_HOME=$HOME/NodeJS/.local/share/pnpm
fi
if [ -d $HOME/ffmpeg ];then
    export PATH=$PATH:$HOME/ffmpeg
fi
if [ -d $HOME/Redis-Server ];then
    export PATH=$PATH:$HOME/Redis-Server
fi
if [ -d $HOME/JRE17 ];then
    export PATH=$PATH:$HOME/JRE17/bin
    export JAVA_HOME=$HOME/JRE17
fi

function TmuxLs(){
Tmux_Name="$1"
tmux_windows=$(tmux ls 2>&1)
if echo ${tmux_windows} | grep -q ${Tmux_Name}
then
    return 0
else
    return 1
fi
}

RedisServerStart(){
Redis=$(redis-cli ping)
if [ ! "${Redis}" = "PONG" ]; then
    tmux new -s "redis" -d "redis-server"
fi
}
RunningState(){
if TmuxLs ${Tmux_Name}
then
    return 1
elif ps all | sed /grep/d | grep -q "${BOT_COMMAND}"
then
    return 2
elif pnpm pm2 list | grep -q ${Bot_Name}
then
    return 3
else
    return 0
fi
}
isRunning(){
RunningState
res=$?
if [ ${res} -eq 1 ];then
    echo -e ${yellow}程序已在TMUX窗口运行${background}
    return 0
elif [ ${res} -eq 2 ];then
    echo -e ${yellow}程序已在前台运行${background}
    return 0
elif [ ${res} -eq 3 ];then
    echo -e ${yellow}程序已在后台运行${background}
    return 0
else
    return 1
fi
}
StopRunning(){
RunningState
res=$?
if [ ${res} -eq 1 ];then
  echo -e ${yellow}正在停止${Bot_Name}${background}
  tmux kill-session -t ${Tmux_Name} > /dev/null 2>&1
  return 0
elif [ ${res} -eq 2 ];then
  echo -e ${yellow}正在停止${Bot_Name}${background}
  PID=$(ps all | sed /grep/d | grep "${BOT_COMMAND}" | awk '{print $3}')
  kill -9 ${PID}
  return 0
elif [ ${res} -eq 3 ];then
  echo -e ${yellow}正在停止${Bot_Name}${background}
  pnpm pm2 stop ${Bot_Name}
  pnpm pm2 delete ${Bot_Name}
  return 0
else
  return 1
fi
}
ChooseStartWay(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的启动方式${background}
echo -e  ${green} 1. ${cyan}前台启动${background}
echo -e  ${green} 2. ${cyan}后台启动${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in
    1)
        BOT start
        ;;
    2)
        BOT BackstageStart
        ;;
esac
}

BOT(){
case "$1" in
  start)
    if RunningState;then
      echo -en ${green}${Bot_Name}已启动 ${cyan}回车返回${background}
      read
      return
    fi
    RedisServerStart
    node app
    ;;
  BackstageStart)
    if RunningState;then
      echo -en ${green}${Bot_Name}已启动 ${cyan}回车返回${background}
      read
      return
    fi
    RedisServerStart
    if tmux new -s ${Tmux_Name} -d "node app"
    then
      echo -en ${green}启动成功 ${cyan}回车返回${background}
      read
    else
      echo -en ${red}启动失败 ${cyan}回车返回${background}
      read
    fi
    ;;
  stop)
    if StopRunning
    then
      echo -en ${green}停止成功 ${cyan}回车返回${background}
      read
    else
      echo -en ${red}停止失败 ${cyan}回车返回${background}
      read
    fi
    ;;
  restart)
    if RunningState
    then
      echo -e ${cyan}正在重启${Bot_Name}${background}
      StopRunning
      RedisServerStart
      ChooseStartWay
      return
    else
      echo -e ${red}${Bot_Name}未运行${background}
    fi
    ;;
  log)
    if TmuxLs ${Tmux_Name};then
      tmux attach -t ${Tmux_Name} > /dev/null 2>&1
    elif pnpm pm2 list | grep -q ${Bot_Name};then
      pnpm pm2 log ${Bot_Name}
    else
      if ps all | sed /grep/d | grep -q "${BOT_COMMAND}"
      then
        echo -en ${cyan}${Bot_Name}已前台启动${background}
        read
        return
      else
        echo -en ${red}${Bot_Name}未启动${background}
        read
        return
      fi
    fi
    echo -en ${cyan}回车返回${background}
    read
    ;;
esac
}

GitUpdate(){
cd ${Bot_Path}
git_pull(){
echo -e ${yellow}正在更新 ${Name}${background}
if ! git pull -f
then
    echo -en ${red}${Name}更新失败 ${yellow}是否强制更新 [Y/N]${background};read YN
    case ${YN} in
    Y|y)
        remote=$(grep 'remote =' .git/config | sed 's/remote =//g')
        remote=$(echo ${remote})
        branch=$(grep branch .git/config | sed "s/\[branch \"//g" | sed 's/"//g' | sed "s/\]//g")
        branch=$(echo ${branch})
        git fetch --all
        git reset --hard ${remote}/${branch}
        git_pull
    esac
fi
}
Name=${Bot_Name}
git_pull
for folder in $(ls plugins)
do
    if [ -d plugins/${folder}/.git ];then
        cd plugins/${folder}
        Name=${folder}
        git_pull
        cd ../../
    fi
done
echo -e ${yellow}正在更新NPM${background}
npm install -g npm@latest
echo -e ${yellow}正在更新PNPM${background}
pnpm install -g pnpm@latest
echo -en ${cyan}更新完成 回车返回${background};read
}

QSignAPIChange(){
if [ ${Bot_Name} == "TRSS-Yunzai" ];then
    echo -e ${cyan}TRSS崽请直接使用 ${yellow}"#QQ签名 + 签名服务器地址" ${background}
    echo -en ${cyan}回车返回${background};read
    return
fi
echo -e ${white}"====="${green}白狐-QSign${white}"====="${background}
echo "========================="
echo -e ${green}请输入您的新签名服务器链接: ${background};read API
file=config/config/bot.yaml
old_sign_api_addr=$(grep sign_api_addr ${file})
new_sign_api_addr="sign_api_addr: ${API}"
sed -i "s|${old_sign_api_addr}|${new_sign_api_addr}|g" ${file}
API=$(grep sign_api_addr ${file})
API=$(echo ${API} | sed "s/sign_api_addr: //g")
echo -e ${cyan}您的API链接已修改为:${green}${API}${background}
echo
echo -en ${cyan}回车返回${background};read
}

function Main(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的操作[${Bot_Name}]${background}
echo -e  ${green} 1. ${cyan}前台启动${background}
echo -e  ${green} 2. ${cyan}后台启动${background}
echo -e  ${green} 3. ${cyan}停止运行${background}
echo -e  ${green} 4. ${cyan}重新启动${background}
echo -e  ${green} 5. ${cyan}打开日志${background}
echo -e  ${green} 6. ${cyan}插件管理${background}
echo -e  ${green} 7. ${cyan}全部更新${background}
echo -e  ${green} 8. ${cyan}填写签名${background}
echo -e  ${green} 9. ${cyan}其他功能${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in
    1)
        BOT start
        ;;
    2)
        BOT BackstageStart
        ;;
    3)
        BOT stop
        ;;
    4)
        BOT restart
        ;;
    5)
        BOT log
        ;;
    6)
        echo -e ${cyan}因为还在搞镜像源的原因 所以暂时不可用${background}
        echo -en ${cyan}回车返回${background};read
        ;;
    7)
        GitUpdate
        ;;
    8)
        QSignAPIChange
        ;;
    9)
        bash ../Script/OtherFunctions.sh
        ;;
esac
}
function BOT_INSTALL(){
bash Script/InstallPkg.sh
bash Script/GitBot.sh
Bot_Path
}
function Bot_Path(){
    if [ -d $HOME/${Bot_Name}/node_modules ]
    then
        export Bot_Path=$HOME/${Bot_Name}
        cd ${Bot_Path}
        Main
    else
        BOT_INSTALL
    fi
}
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的BOT或者操作${background}
echo -e  ${green} 1. ${cyan}Miao-Yunzai${background}
echo -e  ${green} 2. ${cyan}TRSS-Yunzai${background}
echo -e  ${green} 3. ${cyan}API签名管理${background}
echo -e  ${green} 4. ${cyan}卸载-or-移除${background}
echo -e  ${green} 5. ${cyan}检查设备环境${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in
    1)
        export Bot_Name="Miao-Yunzai"
        BOT_COMMAND="Miao-Yun"
        Tmux_Name=MZ
        Bot_Path
        ;;
    2)
        export Bot_Name="TRSS-Yunzai"
        BOT_COMMAND="TRSS Yun"
        Tmux_Name=TZ
        Bot_Path
        ;;
    3)
        bash Script/QSignServer.sh
        ;;
    4)
        bash Script/Remove.sh
        ;;
    5)
        bash Script/InstallPkg.sh
        ;;
    *)
        echo -e ${red}输入错误${background}
        exit
        ;;
esac